<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 300px;
            height: 300px;
            border: 2px solid red;
        }
    </style>
</head>
<body>
    <button>点我</button>
    <div></div>
    <script>
        const btn = document.querySelector('button');
        const box = document.querySelector('div');
        btn.onclick = function(){
            //创建对象
            const xhr = new XMLHttpRequest();
            //初始化 设置请求方法和url
            xhr.open('GET','http://127.0.0.1:8000/server?t='+Date.now());
            //发送
            xhr.send();
            // 事件绑定 处理服务端返回的结果
            // readystate 是xhr对象中的属性 返回值0 1 2 3 4
            xhr.onreadystatechange = function(){
                //判断服务端返回了全部的结果
                if(xhr.readyState === 4){
                    // 判断服务响应码 200 404 
                    if(xhr.status >= 200 && xhr.status < 300){
                        //响应行 
                        console.log(xhr.status);//状态码
                        console.log(xhr.statusText);//状态字符串
                        console.log(xhr.getAllResponseHeaders());//所有的响应头
                        console.log(xhr.response);//响应体
                        box.innerHTML = xhr.response;
                    }
                }
            }
        }
    </script>
</body>
</html>